package com.gjy.concurrency.use;

import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-03-08 10:28:17
 */
public class LockSupportTest {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
        }, "A");
        t1.start();

        new Thread(() -> {
//            LockSupport.unpark(t1);
            System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
//            t1.interrupt();
            t1.interrupt();
        }, "B").start();
    }

    @Test
    public void test2() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            System.out.println("A");
            LockSupport.park();
            System.out.println("B");
        });
        t1.start();

        TimeUnit.SECONDS.sleep(1);
        System.out.println("H");
        LockSupport.unpark(t1);
    }

    @Test
    public void test1() {
        Thread t1 = new Thread(() -> {
            LockSupport.park();
            System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
        }, "A");
        t1.start();

        new Thread(() -> {
//            LockSupport.unpark(t1);
            System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
//            t1.interrupt();

        }, "B").start();
    }
}
